package cn.iocoder.yudao.module.liveprice.service;

import cn.iocoder.yudao.module.liveprice.api.LivePriceApi;
import cn.iocoder.yudao.module.liveprice.api.dto.LivePriceRespDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class PriceBroadcast {

    @Autowired
    private LivePriceApi livePriceApi;


    //    @Scheduled(fixedRate = 3000)
    public Map<String, Object> broadcastPrice() {
        List<LivePriceRespDTO> list = new ArrayList<>(20);
//        for (ProductMaterialTypeEnum material : ProductMaterialTypeEnum.values()) {
//            list.add(livePriceApi.getLivePrice(ProductTypeEnum.MALL.getType(), material.getType()));
//            list.add(livePriceApi.getLivePrice(ProductTypeEnum.RECLAIM.getType(), material.getType()));
//            list.add(livePriceApi.getLivePrice(ProductTypeEnum.MATERIAL.getType(), material.getType()));
//        }

        Map<String, Object> data = new HashMap<>();
        data.put("confPrices", convertPricesToFrontendFormat(list, false));
        data.put("realPrices", convertPricesToFrontendFormat(list, true));
        return data;
    }

    private Map<String, Object> convertPricesToFrontendFormat(List<LivePriceRespDTO> prices, boolean isReal) {
        Map<String, Object> formattedPrices = new HashMap<>();
//        formattedPrices.put(ProductTypeEnum.MALL.getTag(), new HashMap<String, Map<String, Object>>());
//        formattedPrices.put(ProductTypeEnum.MATERIAL.getTag(), new HashMap<String, Map<String, Object>>());
//        formattedPrices.put(ProductTypeEnum.RECLAIM.getTag(), new HashMap<String, Map<String, Object>>());
//
//        for (LivePriceRespDTO price : prices) {
//            String materialName = price.getMaterialName();
//            String category = ProductTypeEnum.getEnum(price.getModuleType()).getTag();
//
//            Map<String, Object> priceData = new HashMap<>();
//            priceData.put("salePrice", isReal ? price.getSalePriceReal() : price.getSalePrice());
//            priceData.put("reclaimPrice", isReal ? price.getReclaimPriceReal() : price.getReclaimPrice());
//            priceData.put("dateTime", price.getDateTime().toString());
//
//            ((Map<String, Map<String, Object>>) formattedPrices.get(category)).put(materialName, priceData);
//        }

        return formattedPrices;
    }
}